package day15.date;

import org.junit.Test;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @Auther:小陈的Java
 */
public class DateTest {
    @Test
    public void dateTest1(){
        Date date = new Date();
        System.out.println(date);
    }
    @Test
    public void dateTest2(){
        Date date1 = new Date();
        Date date2 = new Date(1000L);
        System.out.println(date1.after(date2));
        System.out.println(date1.before(date2));
        System.out.println(date2.getTime());
    }
    @Test
    public void dateTest3(){
        Date date1 = new Date();//java.util.Date 转 java.sql.Date
        java.sql.Date date2 = new java.sql.Date(date1.getTime());
        // java.sql.Date  转 //java.util.Date 向上转换
        Date date3 = new Date();
        System.out.println(date3);
    }
    @Test
    public void dateTest4(){
        //java.util.Date 转 java.sql.Timestamp
        Date date1 = new Date();
        Timestamp date2 = new Timestamp(date1.getTime());
        // java.sql.Timestamp  转 //java.util.Date 向上转换
        Date date3 = date2;
        System.out.println(date3);
    }
    @Test
    public void dateTest5() throws Exception{
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = simpleDateFormat.format(date);
        System.out.println(result);

        String str = "2022/10/1 23:59:59";
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date2 = simpleDateFormat1.parse(str);
        System.out.println(date2);
    }
    @Test
    public void calenderTest1(){
        Calendar calendar = new GregorianCalendar();
        Calendar calendar1 = Calendar.getInstance();
        System.out.println(calendar1);

    }
    @Test
    public void calenderTest2(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,2021);
        calendar.set(Calendar.MONDAY,1);
        calendar.set(Calendar.DAY_OF_MONTH,8);
        calendar.set(Calendar.HOUR_OF_DAY,22);
        calendar.set(Calendar.MINUTE,22);
        calendar.set(Calendar.SECOND,22);
        calendar.set(2021,3,5);
        calendar.set(2021,3,5,5,4,6);

        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));

        Date date = calendar.getTime();
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

        Calendar calendar1 = Calendar.getInstance();

        calendar1.setTime(new Date(1000L));

        System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date));

    }
}
